#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import parseSymDiffReports as PSD
import diffSymbolDB


#扫描指定目录并返回目录下所有文件列表
def ScanDir2FileList(filepath):
    filelist=[f for f in os.listdir(filepath) if os.path.isfile(os.path.join(filepath,f))]
    return filelist

#将lib库名字小版本号剔除
def TransDumpName2OrgLibName(Dumplist):
    resultList = dict()
    for item in Dumplist:
        post=item.find(".so")
        if post != -1 :
            resultList[item[:post + 3]] = item
    return resultList

#针对版本号在.so前面的情况
def getPartBeforeSecondDash(Dumplist):
    resultList = dict()
    for item in Dumplist:
        first_dash = item.find('-')
        second_dash = item.find('-', first_dash + 1)
        if second_dash != -1:
            resultList[item[:second_dash]] = item
    return resultList

def GetLibNameByDumpFile(dumpfile):
    filename=os.path.basename(dumpfile)
    result=filename[4:]
    post = result.find('.dump')
    return result[:post]

def CompareDumpFile2SymDiff(libname,old_dump,new_dump):
    CMD="abi-compliance-checker -limit-affected 1000 -l %s -old %s -new %s -keep-reserved -keep-cxx -old-style -binary -report-format xml" % (libname,old_dump,new_dump)
    print(CMD)
    os.system(CMD)

def ConvertDump2SymDiffByDumpSet(cq24Dir,centosDir):
    cendict = dict()
    cq24dict = dict()

    cq24list=ScanDir2FileList(cq24Dir)
    cenlist=ScanDir2FileList(centosDir)

    cendict =  TransDumpName2OrgLibName(cenlist)
    cq24dict  =  TransDumpName2OrgLibName(cq24list)
    
    cendict.update(getPartBeforeSecondDash(cenlist))
    cq24dict.update(getPartBeforeSecondDash(cq24list))
    # cendict = {k: v for d in [cendict, getPartBeforeSecondDash(cenlist)] for k, v in d.items()}
    # cq24dict = {k: v for d in [cq24dict, getPartBeforeSecondDash(cq24list)] for k, v in d.items()}
    dir_root=os.path.dirname(os.path.realpath(__file__))
    error_log_path = os.path.join(dir_root, "missing_reports.txt")  # 定义错误日志文件路径
    for key,value in cq24dict.items():
        if not key in cendict.keys():
            continue
        old_dumpfile="%s/%s" % (centosDir,cendict[key])
        new_dumpfile="%s/%s" % (cq24Dir,value)
        CompareDumpFile2SymDiff(os.path.basename(old_dumpfile),old_dumpfile,new_dumpfile)
        cenlib=GetLibNameByDumpFile(old_dumpfile)
        cqlib=GetLibNameByDumpFile(new_dumpfile)
        # 这里根据关联表去获取cen_libname的名称(因为这里剔除了版本号，需要模糊匹配)
        cenlib_name=diffSymbolDB.selectCenLibnameBylibRPMrelevance(cenlib)
        # 这里根据关联表获取cq_libname的名称
        cqlib_name=diffSymbolDB.selectCqLibnameBylibRPMrelevance(cqlib)
        # PSD.ParseXmlEleMent2DB("{}/compat_reports/{}/X_to_Y/abi_compat_report.xml".format(dir_root,os.path.basename(old_dumpfile)),cenlib,cqlib)
        report_path = "{}/compat_reports/{}/X_to_Y/abi_compat_report.xml".format(dir_root, os.path.basename(old_dumpfile))
        if os.path.exists(report_path):
            PSD.ParseXmlEleMent2DB(report_path, cenlib_name, cqlib_name)
        else:
            with open(error_log_path, "a") as log_file:
                log_file.write("报告文件不存在:{}\n".format(report_path))



if __name__== "__main__" :
    dir_root=os.path.dirname(os.path.realpath(__file__))
    dir_cen85=dir_root+"/cen7-0-dump/"
    dir_cq24=dir_root+"/ABIDUMP/"
    diffSymbolDB.createTable()
    ConvertDump2SymDiffByDumpSet(dir_cq24,dir_cen85)